package com.ushine.web.template.task.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ushine.web.base.model.base.BaseBO;
import com.ushine.web.component.exception.ServiceException;
import com.ushine.web.component.http.HttpResponseVO;
import com.ushine.web.component.page.PageRequestQuery;
import com.ushine.web.component.page.PageResultVO;
import com.ushine.web.component.task.model.AsyncTaskInfoDO;
import com.ushine.web.component.task.service.IAsyncTaskService;
import com.ushine.web.component.task.template.AsyncTaskTemplate;
import com.ushine.web.log.annotation.SystemControllerLog;
import com.ushine.web.log.constant.LogConstant;
import com.ushine.web.template.mybatis.model.DemoDO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.oas.annotations.EnableOpenApi;

import javax.annotation.Resource;

/**
 * @Author: yls
 * @Date: 2021/04/28 23:25
 * @Description: 任务调度控制器
 * @Version 1.0
 */
@Slf4j
@EnableOpenApi
@RestController
@RequestMapping("component/asyncTask")
@Api(tags = "组件模块-异步任务调度测试")
public class AsyncTaskController {

    @Resource
    AsyncTaskTemplate asyncTaskTemplate;

    @Resource
    IAsyncTaskService asyncTaskService;

    @SystemControllerLog(operation = LogConstant.READ)
    @GetMapping("startTask")
    @ApiOperation(value = "模拟启动一个异步任务")
    public HttpResponseVO<Object> startAsyncTask() throws ServiceException {
        //  初始化任务信息
        AsyncTaskInfoDO<DemoDO> asyncTaskInfo = new AsyncTaskInfoDO<>();
        // 必须指定任务名称：模块名称+具体任务信息
        asyncTaskInfo.setTaskName("组件模块-异步任务调度测试-模拟单个异步任务");
        // 调用任务模板，开启任务
        asyncTaskTemplate.executorRunnable(asyncTaskInfo);
        return new HttpResponseVO<>().ok("成功执行异步任务！");
    }

    @SystemControllerLog(operation = LogConstant.READ)
    @GetMapping("startTasks")
    @ApiOperation(value = "模拟启动多个异步任务")
    public HttpResponseVO<Object> startAsyncTasks() throws ServiceException {
        int taskSize = 60;
        for (int i = 0; i < taskSize; i++) {
            AsyncTaskInfoDO<DemoDO> asyncTaskInfo = new AsyncTaskInfoDO<>();
            asyncTaskInfo.setTaskName("模拟多个异步任务:" + i);
            asyncTaskTemplate.executorRunnable(asyncTaskInfo);
        }
        return new HttpResponseVO<>().ok("成功执行异步任务！");
    }

    @SystemControllerLog(operation = LogConstant.READ)
    @GetMapping("getTaskStatus/{taskId}")
    @ApiOperation(value = "模拟获取任务状态")
    public HttpResponseVO<AsyncTaskInfoDO<BaseBO>> getTaskStatus(@PathVariable("taskId") Long taskId) {
        AsyncTaskInfoDO taskInfo = asyncTaskService.getAsyncTaskInfoById(taskId);
        return new HttpResponseVO<AsyncTaskInfoDO<BaseBO>>().ok("", taskInfo);
    }


    @SystemControllerLog(operation = LogConstant.READ)
    @PostMapping("listAsyncTaskInfoByPage")
    @ApiOperation(value = "分页查询任务")
    public HttpResponseVO<PageResultVO<AsyncTaskInfoDO>> listAsyncTaskInfoByPage(@RequestBody PageRequestQuery pageRequestQuery) {
        Page<AsyncTaskInfoDO> taskInfos = asyncTaskService.listAsyncTaskInfos(pageRequestQuery);
        return new HttpResponseVO<PageResultVO<AsyncTaskInfoDO>>().ok("", taskInfos);
    }
}
